{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1892d60e-5f75-48fc-b762-56f0dd5c5e7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "39318ddd-26e7-4e30-99bb-536ea1b9c541",
   "metadata": {},
   "outputs": [],
   "source": [
    "root_dir = r\"D:\\desktop\\practice\\数据集\\hymenoptera_data\\train\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "97de8e21-1ec5-40ff-b0f9-92b17bfbf41f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0013035.jpg'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_dir = \"ants\"\n",
    "image_path = os.listdir(os.path.join(root_dir,target_dir))\n",
    "image_path[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c0dd85a8-458f-4286-a56f-f1cb3e77756f",
   "metadata": {},
   "outputs": [],
   "source": [
    "label = \"ant\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b2f26055-866c-4035-8c9f-90e8d6fbd7aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(root_dir,\"ants-label\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ee4de845-ce9a-4f5a-8de1-1477b29e8cc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in image_path:\n",
    "    file_name = i.split(\".jpg\")[0]\n",
    "    with open(os.path.join(out_dir,\"{}.txt\".format(file_name)),'w') as f:\n",
    "        f.write(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1db3c8e4-5609-461c-b4a5-5ac7fd06552d",
   "metadata": {},
   "outputs": [],
   "source": [
    "target_dir = \"bees\"\n",
    "image_path = os.listdir(os.path.join(root_dir,target_dir))\n",
    "label = \"bee\"\n",
    "out_dir = os.path.join(root_dir,\"bees-label\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1a74e42f-9d42-4f5a-b214-645d74612b50",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in image_path:\n",
    "    file_name = i.split(\".\")[0]\n",
    "    with open(os.path.join(out_dir,\"{}.txt\".format(file_name)),'w') as f:\n",
    "        f.write(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a4ae9f8-1bb1-4e82-b7dd-236c92a759f3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
